package prepare_data

import (
	"adam2/internal/model"
	"adam2/internal/util"
	"anubis-framework/pkg/io"
	"gorm.io/gorm"
)

// 准备数据：炸板策略
type PriceLimitBreakdownPrepareData struct {
	db *gorm.DB
}

// 初始化
func (p *PriceLimitBreakdownPrepareData) Init(db *gorm.DB) {
	p.db = db
}

// 准备数据：清空quant_stock_filter表，从stock_transaction_data_all表中向quant_stock_filter表中插入记录，删除非炸板的股票
func (p *PriceLimitBreakdownPrepareData) DoPrepareData(currentDate string) {
	io.Infoln("准备数据：清空quant_stock_filter表，从stock_transaction_data_all表中向quant_stock_filter表中插入记录，删除非炸板的股票")

	// 清空quant_stock_filter表
	p.db.Exec("truncate table quant_stock_filter")

	// 从stock_transaction_data_all表中向quant_stock_filter表中插入记录，不包括非炸板的股票
	var stockTransactionDataAllArray *model.StockTransactionDataAllArray
	p.db.Raw("select * from stock_transaction_data_all stda where stda.date_=to_date(?, 'yyyy-mm-dd')", currentDate).Scan(&stockTransactionDataAllArray)
	if stockTransactionDataAllArray != nil && len(*stockTransactionDataAllArray) > 0 {
		for _, stockTransactionDataAll := range *stockTransactionDataAllArray {
			var priceLimitBreakdown bool = util.PriceLimitBreakdown(*stockTransactionDataAll, currentDate)
			if priceLimitBreakdown == true {
				p.db.Exec("insert into quant_stock_filter(stock_code) values(?)", stockTransactionDataAll.Code)
			}
		}
	}

}
